<?php 
if (!session_id()) {
    session_start();
}
if(isset($_POST['result'])){
    global $wpdb;
    $tb = 'ap_painting_user' ;
    $user = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE user_id = {$_SESSION['user_id']}");
    $data = array('turn_spin'=> $user->turn_spin - 1);
    $wpdb->update($tb,$data,array( 'id' => $user->id ));

    if($_POST['result']=='true'){
	    $tb = 'ap_painting_exam';
	    $user_id = $_SESSION['user_id'];
	    $data = array('user_id' => $user_id, 'gift' => 'Quà', 'dateadd' => time());
	    $wpdb->insert($tb, $data,array('%s', '%s', '%s'));
	}
    exit;
}
get_header();
?>
<?php
	if(!isset($_SESSION['buoc_2'])||$_SESSION['buoc_2']!=true){
		wp_redirect( home_url() ); exit;
	}
    global $wpdb;
    $user = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE user_id = {$_SESSION['user_id']}");
    $remain = $user->turn_spin;
?>
<script type="text/javascript">
  $('.container').addClass('intro buoc-3');
</script>

<div class="content-quayso">
	<img class="puzzle2" alt="<?php echo $nameimg ?>" src="<?php echo bloginfo('template_url'); ?>/img/bg-quay-top.png" />
	<div class="avatar2">
		<img class="element-animation2" src="<?php echo bloginfo('template_url'); ?>/img/avatar2.png">
	</div>
	<img class="frame" src="<?php echo bloginfo('template_url'); ?>/img/bg-quay-mask.png">
</div>
<div class="ok-btn">
   <a href="javascript:;"><img src="<?php echo bloginfo('template_url'); ?>/img/btn-ok2.png"></a>
</div>

<script type="text/javascript">
function randomFromInterval(from,to){
    return Math.floor(Math.random()*(to-from+1)+from);
}

var site_url = '<?php echo site_url() ?>';
$('.ok-btn').live('click',function(){
    init_roll();
});
var remain = <?php echo $remain; ?>;
var processing = false;
var random1 = -1;
var random2 = -1;
var random3 = -1;

function calculate(){
	var ratio = 20;
	var random = randomFromInterval(0,100);
	var result = false;
	if(random<=ratio){ // trung
		random1 = randomFromInterval(1,7);
		random2 = random3 = random1;
		result = true;
	}else{ // trat
		random1 = randomFromInterval(1,7);
		random2 = (random1+2)%7+1;
		random3 = (random2+2)%7+1;
		result = false;
	}
	$.ajax({
        type: "POST",
        url: '<?php echo site_url() ?>/quay-so',
        data: {
        	result: result
        }
    });
}
</script>
<!-- |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| -->



<div class="content tranhtai" style="display:none;">
	<img class="puzzle2" alt="<?php echo $nameimg ?>" src="<?php echo bloginfo('template_url'); ?>/img/bg-quay-bottom.png" />
	<div class="col col-1">
		<?php for($i=1;$i<=8;$i++){ ?>
		<img data="<?php echo $i ?>" src="<?php echo bloginfo('template_url'); ?>/images/thumb/cake<?php echo $i==8?1:$i; ?>.png">
		<?php } ?>
	</div>
	<div class="col col-2">
		<?php for($i=1;$i<=8;$i++){ ?>
		<img data="<?php echo $i ?>" src="<?php echo bloginfo('template_url'); ?>/images/thumb/cake<?php echo $i==8?1:$i; ?>.png">
		<?php } ?>
	</div>
	<div class="col col-3">
		<?php for($i=1;$i<=8;$i++){ ?>
		<img data="<?php echo $i ?>" src="<?php echo bloginfo('template_url'); ?>/images/thumb/cake<?php echo $i==8?1:$i; ?>.png">
		<?php } ?>
	</div>
	<img class="frame" src="<?php echo bloginfo('template_url'); ?>/img/bg-quay.png">
	<div class="message" style="display:none;"><img src="<?php echo bloginfo('template_url'); ?>/img/trat.png"></div>
	<div class="btn-home"><a href="<?php echo site_url() ?>"><img src="<?php echo bloginfo('template_url'); ?>/img/btn-home.png"></a></div>
	<div class="btn-start"><a href="javascript:;"><img src="<?php echo bloginfo('template_url'); ?>/img/btn-create.png"></a></div>
	<div class="btn-turn"><img src="<?php echo bloginfo('template_url'); ?>/img/btn-turn.png"><span class="remain"><?php echo $remain<10?'0'.$remain:$remain; ?></span></div>
</div>
<script type="text/javascript">
	 function init_roll(){
	 	$('.content-quayso').hide();
        $('.ok-btn').hide();
        $('.content.tranhtai').show();
	 }
	var finish1 = false;
	var finish2 = false;
	var finish3 = false;

	var count1 = 0;
	var while1 = '';
	function rolling1(){
	 	var v = 19;
	 	var i = count1;
	 	var col = 1;
 		var time = 13*(7+random1);

	 	$('.col-'+col+' img').css('top',(v*i)%1705+'px');

	 	count1++;
   		if(count1>=time-2){
   			finish1 = true;
   			clearInterval(while1);
   			if(finish1&&finish2&&finish3){
   				if(random1==random2&&random2==random3){
			 		message_popup(1);
			 	}else{
			 		message_popup(0);
			 	}
   			}
   		}
	}

	var count2 = 0;
	var while2 = '';
	function rolling2(){
	 	var v = 19;
	 	var i = count2;
	 	var col = 2;
 		var time = 13*(7+random2);

	 	$('.col-'+col+' img').css('top',(v*i)%1705+'px');

	 	count2++;
   		if(count2>=time-2){
   			finish2 = true;
   			clearInterval(while2);
   			if(finish1&&finish2&&finish3){
   				if(random1==random2&&random2==random3){
			 		message_popup(1);
			 	}else{
			 		message_popup(0);
			 	}
   			}
   		}
	}

	var count3 = 0;
	var while3 = '';
	function rolling3(){
	 	var v = 19;
	 	var i = count3;
	 	var col = 3;
 		var time = 13*(7+random3);

	 	$('.col-'+col+' img').css('top',(v*i)%1705+'px');

	 	count3++;
   		if(count3>=time-2){
   			finish3 = true;
   			clearInterval(while3);
   			if(finish1&&finish2&&finish3){
   				if(random1==random2&&random2==random3){
			 		message_popup(1);
			 	}else{
			 		message_popup(0);
			 	}
   			}
   		}
	}
	function processing_roll(){
	 	processing = true;
	 	remain--;
	 	str_remain = remain;
	 	if(remain<10) str_remain = '0'+remain;
	 	$('.remain').html(str_remain);
	 	// rolling
	 	calculate();
		while1 = setInterval(rolling1, 5);
		while2 = setInterval(rolling2, 10);
		while3 = setInterval(rolling3, 20);
	 	// rolling
	 }
	 function message_popup(type){
	 	if(type==0){
	 		$('.message img').attr('src','<?php echo bloginfo('template_url'); ?>/img/trat.png');
	 	}else{
	 		$('.message img').attr('src','<?php echo bloginfo('template_url'); ?>/img/trung.png');
	 	}
	 	$('.message').fadeIn();
	 	processing = false;
	 	$('.btn-start a img').attr('src',"<?php echo bloginfo('template_url'); ?>/img/btn-create.png");
	 }
	 $('.btn-start a').live('mousedown',function(){
		if(remain>0&&processing==false){
			count1 = count2 = count3 = 0;
			finish1 = finish2 = finish3 = false;
			$('.message').hide();
			$('.btn-start a img').attr('src',"<?php echo bloginfo('template_url'); ?>/img/btn-create-on.png");
			processing_roll();
		}
	});
</script>


<?php get_footer() ?>